package com.mqqzt.douban;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.mqqzt.douban.util.NetUtil;

public class LoginActivity extends Activity implements OnClickListener {
	protected static final int NEED_CAPTCHA = 10;
	protected static final int NOT_NEED_CAPTCHA = 11;
	protected static final int GET_CAPTCHA_ERROR = 12;
	protected static final int LOGIN_SUCCESS = 13;
	protected static final int LOGIN_FAIL = 14;
	private EditText mEmailEditText;
	private EditText mPwdEditText;
	private LinearLayout mCaptchaLinearLayout;
	private EditText mCaptchaEditText;
	private ImageView mCaptchaImageView;
	private Button mLoginButton;
	private Button mExitButton;
	private ProgressDialog pd;
	private String result;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			pd.dismiss();
			switch (msg.what) {
			case NEED_CAPTCHA:
				Bitmap bitmap = (Bitmap) msg.obj;
				mCaptchaLinearLayout.setVisibility(View.VISIBLE);
				mCaptchaImageView.setImageBitmap(bitmap);
				break;

			case NOT_NEED_CAPTCHA:

				break;
			case GET_CAPTCHA_ERROR:
				Toast.makeText(LoginActivity.this, "查询验证码失败", Toast.LENGTH_LONG)
						.show();
				break;
			case LOGIN_SUCCESS:
				finish();
				break;
			case LOGIN_FAIL:
				Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG)
						.show();
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		pd = new ProgressDialog(this);
		setupView();
		setListener();
	}

	private void setupView() {
		mEmailEditText = (EditText) this.findViewById(R.id.EditTextEmail);
		mPwdEditText = (EditText) this.findViewById(R.id.EditTextPassword);
		mCaptchaLinearLayout = (LinearLayout) this
				.findViewById(R.id.ll_captcha);
		mCaptchaEditText = (EditText) this
				.findViewById(R.id.EditTextCaptchaValue);
		mCaptchaImageView = (ImageView) this
				.findViewById(R.id.ImageViewCaptcha);
		mLoginButton = (Button) this.findViewById(R.id.btnLogin);
		mExitButton = (Button) this.findViewById(R.id.btnExit);
		// 判断是否需要验证码
		getCaptcha();
	}

	private void getCaptcha() {

		pd.setMessage("正在查询验证码");
		pd.show();
		new Thread() {

			@Override
			public void run() {
				try {
					result = NetUtil.isNeedaptcha(getApplicationContext());
					if (result != null) {
						String imagePath = getResources().getString(
								R.string.captchaurl)
								+ result + "&amp;size=s";
						Bitmap bitmap = NetUtil.getImage(imagePath);
						Message msg = Message.obtain();
						msg.what = NEED_CAPTCHA;
						msg.obj = bitmap;
						handler.sendMessage(msg);
					} else {
						Message msg = Message.obtain();
						msg.what = NOT_NEED_CAPTCHA;
						handler.sendMessage(msg);
					}
				} catch (Exception e) {
					e.printStackTrace();
					Message msg = Message.obtain();
					msg.what = GET_CAPTCHA_ERROR;
					handler.sendMessage(msg);
				}
			}
		}.start();
	}

	private void setListener() {
		mLoginButton.setOnClickListener(this);
		mExitButton.setOnClickListener(this);
		mCaptchaImageView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnLogin:
			String name = mEmailEditText.getText().toString().trim();
			String password = mPwdEditText.getText().toString().trim();
			if ("".equals(name) || "".equals(password)) {
				Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
				return;
			}
			if (result != null) {
				String captchaValue = mCaptchaEditText.getText().toString()
						.trim();
				if ("".equals(captchaValue)) {
					Toast.makeText(this, "验证码不能为空", Toast.LENGTH_LONG).show();
					return;
				} else {
					login(name, password, captchaValue);
				}
			} else {
				login(name, password, "");
			}

			break;

		case R.id.btnExit:
			finish();
			break;

		case R.id.ImageViewCaptcha:
			getCaptcha();
			break;
		}

	}

	private void login(final String name, final String pwd, final String captcha) {
		pd.setMessage("正在登录");
		pd.show();
		new Thread() {

			@Override
			public void run() {
				try {
					boolean flag = NetUtil.Login(name, pwd, captcha, result,
							getApplicationContext());
					if (flag) {
						Message msg = Message.obtain();
						msg.what = LOGIN_SUCCESS;
						handler.sendMessage(msg);
					} else {
						Message msg = Message.obtain();
						msg.what = LOGIN_FAIL;
						handler.sendMessage(msg);
					}
				} catch (Exception e) {
					e.printStackTrace();
					Message msg = Message.obtain();
					msg.what = LOGIN_FAIL;
					handler.sendMessage(msg);
				}
			}
		}.start();
	}
}
